{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 基本概念"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 数  \n",
    "数就是日常生活用到的数字，Python中比较常见的就是整数和浮点数两种。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 变量  \n",
    "变量即变化的量，可以理解为用来存储数据的容器。  \n",
    "**定义变量名遵循的原则：**   \n",
    "- 变量名必须以字母或者下划线开通，由字母、数字和下划线组成  \n",
    "- 变量名的程度不能超过255个字符\n",
    "- 变量名在有限的范围内必须是唯一的\n",
    "- 变量名不能是Pytho的关键字"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 标识符  \n",
    "标识符是用来标识某样东西的名字，命名规则与变量名遵循的规则一致。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 数据类型  \n",
    "Python中的数据类型主要有2中，用type()函数查看具体的类型。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "int"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "float"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(1.0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "str"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(\"hello world\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 输出与输出格式设置  \n",
    "Python中用print关键字进行输出，用str.format()方法对输出格式设定。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hewwlo world\n"
     ]
    }
   ],
   "source": [
    "print(\"hewwlo world\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- **一对一填充**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "我正在学习:Python基础知识\n"
     ]
    }
   ],
   "source": [
    "print('我正在学习:{}'.format('Python基础知识'))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- **多对多填充**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "我正在学习:Python数据分析中的Python基础知识\n"
     ]
    }
   ],
   "source": [
    "print('我正在学习:{}中的{}'.format('Python数据分析','Python基础知识'))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- **浮点数设置**  \n",
    ".2f表示浮点型展示，显示小数点的后两位，也可以是.3f或其他"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2018年中国单身人数约2.00亿\n"
     ]
    }
   ],
   "source": [
    "print(\"{}约{:.2f}亿\".format(\"2018年中国单身人数\",2))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- **百分数设置**  \n",
    ".2%表示以百分比的形式展示，且展示小数点后两位，也可以是.3%或者其他"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "中国男性占总人的的比例:51.90%\n"
     ]
    }
   ],
   "source": [
    "print(\"中国男性占总人的的比例:{:.2%}\".format(0.519))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 缩进与注释"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**缩进**  \n",
    "我们把代码的行首空白部分组成缩进，缩进的目的是为了标识代码块"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**注释**  \n",
    "注释是对代码起到说明作用，并不真正运行。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello world\n"
     ]
    }
   ],
   "source": [
    "#这是单行注释，不执行哦\n",
    "print(\"hello world\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello\n"
     ]
    }
   ],
   "source": [
    "'''\n",
    "方法一：\n",
    "这是多行注释,\n",
    "'''\n",
    "\n",
    "\"\"\"\n",
    "方法二：\n",
    "这是多行注释\n",
    "\"\"\"\n",
    "\n",
    "print(\"hello\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 字符串"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 字符串的概念  \n",
    "字符串是由另个或多个字符串组成的有限串行，用单引号或者双引号括起来"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 字符串的连接  \n",
    "直接用+就可以将两个或两个以上的字符串进行连接。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'我要学习Python'"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"我要学习\"+\"Python\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 字符串的复制  \n",
    "使用操作符*进行字符串的复制"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Python数据分析Python数据分析Python数据分析'"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"Python数据分析\"*3"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 获取字符串的长度  \n",
    "使用len()函数来获取字符串的长度"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "11"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(\"15280000003\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 字符串的查找  \n",
    "字符串查找指查找某一个字符串是否包含在另外一个字符串中，用in或者not in这两个方法均可以实现,返回布尔值。还可以用find方法，返回具体位置，如果不存在则返回-1。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"Python\" in \"Python数据分析\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"Python\" not in \"Python数据分析\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"Python数据分析\".find(\"Python\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-1"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"Python数据分析\".find(\"PHP\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 字符串索引  \n",
    "字符串索引是指通过字符串值所处的位置对值进行索取，字符串的位置是从0开始的"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'P'"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#获取字符串第1位值\n",
    "a  = \"Python数据分析\"\n",
    "a[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'h'"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#获取字符串第4位值\n",
    "a[3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'yt'"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#获取字符串第2位到第4之间的值，且不包括第4位的值\n",
    "a[1:3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Pyt'"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#获取字符串第1到第4之间的值，且不包括第4位\n",
    "a[:3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'数据分析'"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#获取字符串第7位到最后1位的值\n",
    "a[6:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'析'"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#获取字符串最后一位值\n",
    "a[-1]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 字符串分割  \n",
    "用split()函数,返回列表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['a', 'b', 'c']"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"a,b,c\".split(\",\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 移除字符\n",
    "用strip()函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'a'"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#移除空格\n",
    "\" a \".strip()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'a'"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#移除换行\n",
    "\"\\ta\\t\".strip()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'LAMP'"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#移除指定字符\n",
    "\"LAMPk\".strip(\"k\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 数据结构——列表"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 列表的概念\n",
    "- 列表(list)是用来储存一组有序数据元素的数据结构，元素之间用逗号隔开;\n",
    "- 元素包含在方括号内，元素可变;"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 新建列表"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- **建立一个空列表**  \n",
    "单方括号没有任何元素时，列表就是一个空列表  \n",
    "null_list = [ ]\n",
    "- **建立一个int类型列表**  \n",
    "当方括号元素全位int类型时，这个列表就是int类型列表\n",
    "int_list = [1,2,3]\n",
    "- **建立一个str类型列表**  \n",
    "当方括号元素全是str类型时，这个列表就是int类型列表 \n",
    "str_list = [\"a\",\"b\",\"c\"]\n",
    "- **建立一个int+srt 类型列表**  \n",
    "当放括号的元素既有int类型，又有str类型时，这个列表就是int+str类型  \n",
    "int_str_list = [1,2,\"a\",\"b\"] "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 列表的复制\n",
    "列表的复制和字符串类似，用\\*操作符"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3, 1, 2, 3]"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "int_list = [1,2,3]\n",
    "int_list*2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['a', 'b', 'c', 'a', 'b', 'c']"
      ]
     },
     "execution_count": 62,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "str_list = [\"a\",\"b\",\"c\"]\n",
    "str_list*2"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 列表的合并\n",
    "- 利用+操作符\n",
    "- 用extend()函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3, 'a', 'b', 'c']"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#利用+操作符\n",
    "int_list = [1,2,3]\n",
    "str_list = [\"a\",\"b\",\"c\"]\n",
    "int_list + str_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3, 'a', 'b', 'c']"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#用extend()函数\n",
    "int_list = [1,2,3]\n",
    "str_list = [\"a\",\"b\",\"c\"]\n",
    "int_list.extend(str_list)\n",
    "int_list"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 向列表中插入新元素\n",
    "- append()在列表末尾插入数据元素\n",
    "- insert()在列表指定位置插入元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3, 4]"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#在末尾插入元素\n",
    "int_list = [1,2,3]\n",
    "int_list.append(4)\n",
    "int_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3, 4]"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#指定位置插入元素\n",
    "int_list = [1,2,3]\n",
    "int_list.insert(3,4)#表示在第4位插入元素4，位置从1开始计算\n",
    "int_list"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 获取列表值出现的次数\n",
    "利用count( )函数获取某个值在列表出现的次数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "score_list = [\"一班\",\"二班\",\"三班\",\"二班\",\"一班\",\"一班\"]\n",
    "score_list.count(\"一班\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 获取列表值出现的位置\n",
    "- 获取值的出现的位置，就是看该值位于列表中的哪里;\n",
    "- 用index()方法，位置从**0**开始"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "car_list =[\"保时捷\",\"法拉利\",\"兰博基尼\",\"宝马\",\"奔驰\",\"奥迪\"]\n",
    "car_list.index(\"兰博基尼\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 获取列表中指定位置的值\n",
    "- 普通索引（获取某一特定位置的数）\n",
    "- 切片索引（获取某一位置区间的数）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'a'"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#普通索引\n",
    "str_list = [\"a\",\"b\",\"c\",\"d\",\"e\",\"f\",\"g\"]\n",
    "str_list[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['b', 'c']"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#切片索引\n",
    "#获取第2位到第4位的数，不包含第4位\n",
    "str_list[1:3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['a', 'b', 'c']"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#获取开始到第4位的数，不包含第4位\n",
    "str_list[:3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['b', 'c', 'd', 'e', 'f', 'g']"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#获取第2位到结束的数\n",
    "str_list[1:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'g'"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#获取最后一位数\n",
    "str_list[-1]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 删除列表元素\n",
    "- pop()函数是指定位置删除,返回**删除的值**\n",
    "- remove()函数是元素进行删除，**没有返回值**,但是会移除列表中的某个值的**第一个**匹配项"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'b'"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#根据位置删除指定元素的\n",
    "str_list = [\"a\",\"b\",\"c\",\"d\",\"e\",\"f\",\"g\"]\n",
    "str_list.pop(1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [],
   "source": [
    "#根据值删除元素\n",
    "str_list = [\"a\",\"b\",\"c\",\"d\",\"e\",\"f\",\"g\",\"a\"]\n",
    "str_list.remove(\"a\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['b', 'c', 'd', 'e', 'f', 'g']"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#删除列表中所有的\"a\"\n",
    "str_list = [\"a\",\"b\",\"c\",\"d\",\"e\",\"f\",\"g\",\"a\"]\n",
    "k = \"a\"\n",
    "while k in str_list:\n",
    "  str_list.remove(k)\n",
    "str_list"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 对列表中的值进行排序\n",
    "用sort()函数进行排序，默认是升序排列,传入参数**reverse=True**可以降序"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 4, 5, 6, 9]"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#默认升序\n",
    "s = [1,5,2,9,4,6]\n",
    "s.sort()\n",
    "s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[9, 6, 5, 4, 2, 1]"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#传参reverse=True降序\n",
    "s = [1,5,2,9,4,6]\n",
    "s.sort(reverse = True)\n",
    "s"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 数据结构——字典"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 字典的概念\n",
    "字典(dict)是一种键值对的结构\n",
    "- 键必须唯一\n",
    "- 键值对内部用冒号分隔\n",
    "- 各键值对用逗号分隔\n",
    "- 所有数据放在花括号中"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 新建一个字典"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'张三': 15280001000, '李四': 13980001000}"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#向空白字典插入值\n",
    "test_dict = {}\n",
    "test_dict[\"张三\"] = 15280001000\n",
    "test_dict[\"李四\"] = 13980001000\n",
    "test_dict"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'张三': 15280001000, '李四': 13980001000}"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#直接将键值对传入字典中\n",
    "test_dict = {\"张三\": 15280001000, \"李四\": 13980001000}\n",
    "test_dict"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'张三': 1528000100, '李四': 13980001000}"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 将键值对以列表的形式存放在元组中，然后用dict进行转换\n",
    "contact = ([\"张三\",1528000100],[\"李四\",13980001000])\n",
    "test_dict = dict(contact)\n",
    "test_dict"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 字典中keys(),values()和items()方法\n",
    "- keys()方法用来获取字典内所有的键\n",
    "- values()方法用来获取字典内所有的值\n",
    "- items()方法用来获得到一组组的键值对"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dict_keys(['张三', '李四'])"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test_dict = {\"张三\": 15280001000, \"李四\": 13980001000}\n",
    "test_dict.keys()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dict_values([15280001000, 13980001000])"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test_dict.values()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dict_items([('张三', 15280001000), ('李四', 13980001000)])"
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test_dict.items()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 数据结构——元组"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 元组的概念\n",
    "元组(tup)与类表类似\n",
    "- 元组的元素不能修改\n",
    "- 元祖用小括号，列表用中括号"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 新建元组\n",
    "直接将一组元素用小括号括起来即可  \n",
    "tup = (1,2,3)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 获取元组的长度\n",
    "使用len()函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 60,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tup = (1,2,3)\n",
    "len(tup)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 获取元组内的元素\n",
    "元组内元素的获取主要分为普通索引和切片索引与列表类似"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 66,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#普通索引\n",
    "tup = (1,2,3)\n",
    "tup[2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(1, 2)"
      ]
     },
     "execution_count": 67,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#切片索引\n",
    "tup = (1,2,3)\n",
    "tup[:2]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 元组与列表相互转换\n",
    "- list()函数将元组转为列表\n",
    "- tuple()函数将列表转为元组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3]"
      ]
     },
     "execution_count": 69,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#将元组转为列表\n",
    "tup = (1,2,3)\n",
    "list(tup)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(1, 2, 3)"
      ]
     },
     "execution_count": 70,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#将列表转为元组\n",
    "t_list = [1,2,3]\n",
    "tuple(t_list)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### zip函数\n",
    "zip()函数用于将可迭代的对象（列表、元组）作为参数，将对象中对于的元素打包成一个个元组，然后返回这些元组组成的列表。常与for循环一起搭配使用。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(1, 'a')\n",
      "(2, 'b')\n",
      "(3, 'c')\n",
      "(4, 'd')\n"
     ]
    }
   ],
   "source": [
    "#可迭代对象是列表时：\n",
    "list_a = [1,2,3,4]\n",
    "list_b = [\"a\",\"b\",\"c\",\"d\"]\n",
    "for i in zip(list_a,list_b):\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(1, 'a')\n",
      "(2, 'b')\n",
      "(3, 'c')\n",
      "(4, 'd')\n"
     ]
    }
   ],
   "source": [
    "#可迭代对象是元组时：\n",
    "list_a = (1,2,3,4)\n",
    "list_b = (\"a\",\"b\",\"c\",\"d\")\n",
    "for i in zip(list_a,list_b):\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 运算符"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 算术运算符  \n",
    "用于算术运算\n",
    "-  \\+ 两数相加\n",
    "-  \\- 两数做差\n",
    "-  \\* 两数相乘\n",
    "-  /  两数相除\n",
    "-  %  返回两数相除的余数\n",
    "-  ** 返回x的y次幂\n",
    "-  // 返回两处相除以后商的**整数**部分"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 比较运算  \n",
    "主要用来比较，返回True或False\n",
    "- == 等于\n",
    "- != 不等于\n",
    "- <> 不等于\n",
    "- \\> 大于\n",
    "- \\< 小于\n",
    "- \\>= 大等于\n",
    "- \\<= 小等于"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 逻辑运算\n",
    "- and 同时为真结果才为真\n",
    "- or 只要一边为真,结果就为真 \n",
    "- not 如果表达式为真,运算结果为False，反之为True"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 循环语句"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### for循环  \n",
    "for循环用来遍历任何序列项目"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "我目前正在学习：Excel\n",
      "我目前正在学习：SQL\n",
      "我目前正在学习：Python\n",
      "我目前正在学习：统计学\n"
     ]
    }
   ],
   "source": [
    "subject = ['Excel','SQL','Python','统计学']\n",
    "for sub in subject:\n",
    "    print('我目前正在学习：{}'.format(sub))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### while循环\n",
    "while循环用来循环执行某程序，，即当条件满足时，一直执行某程序，直到条件不满足时，终止程序；"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "我已经学习数据分析0周啦\n",
      "我已经学习数据分析1周啦\n",
      "我已经学习数据分析2周啦\n",
      "我已经学习数据分析3周啦\n",
      "我已经学习数据分析4周啦\n",
      "我已经学习数据分析5周啦\n",
      "我已经学习数据分析6周啦\n",
      "我已经学习数据分析7周啦\n",
      "我已经学习数据分析7周啦，我可以去找工作啦\n"
     ]
    }
   ],
   "source": [
    "week = 0 #从0开始学\n",
    "while week<= 7:\n",
    "    print(\"我已经学习数据分析{}周啦\".format(week))\n",
    "    week +=1\n",
    "print(\"我已经学习数据分析{}周啦，我可以去找工作啦\".format(week-1))    "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 条件语句"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### if语句\n",
    "- if条件语句是先去判断某个条件是否满足，满足则执行判断后的语句\n",
    "- if条件后面的程序需要首行缩进"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "可以找到好工作了\n"
     ]
    }
   ],
   "source": [
    "is_study = 1\n",
    "if is_study == 1:\n",
    "    print(\"可以找到好工作了\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### else语句  \n",
    "else语句是if语句的补充，即if不满足时执行什么程序"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "可以找到好工作了\n"
     ]
    }
   ],
   "source": [
    "is_study = 1\n",
    "if is_study == 0:\n",
    "    print(\"很难找到好工作了\")\n",
    "else:\n",
    "    print(\"可以找到好工作了\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### elif语句\n",
    "用于多条件判断"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入分数：30\n",
      "小于60分\n"
     ]
    }
   ],
   "source": [
    "score =int(input(\"请输入分数：\"))\n",
    "if score < 60:\n",
    "    print(\"小于60分\")\n",
    "elif score < 70:\n",
    "    print(\"60~70分\")\n",
    "elif score < 80:\n",
    "    print(\"70~80分\")\n",
    "else :\n",
    "    print(\"90~100分\")    "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 函数  \n",
    "函数是在一个程序中被重复使用的一段程序"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 普通函数  \n",
    "- 普通函数一般有函数名（必需）、参数、语句块（必需）、return、变量这几部分组成\n",
    "- 函数定于的语法：  \n",
    "def 函数名(参数):  \n",
    "    语句块"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'我正在地铁上学Python'"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#定义一个return函数\n",
    "def learn_python(location):\n",
    "    doing = (\"我正在{}上学Python\".format(location))#注意{}中间不能敲空格\n",
    "    return doing\n",
    "\n",
    "learn_python(\"地铁\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 匿名函数  \n",
    "匿名函数就是没有名字的函数，省略了def定义函数的过程，lambda至于一个表达式，没有具体的函数体。使用方法如下：  \n",
    "lambda arg1,arg2,arg3,...:expression\n",
    "- arg1,arg2,arg3表示具体参数\n",
    "- expression 表示参数要执行的操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "f = lambda x,y:x+y\n",
    "f(1,2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 高级特性"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 列表生成式 ✩✩✩\n",
    "例：现有一个列表，需要对列表的每个值求平方，组成成一个新列表，对比两种方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 4, 9, 16, 25]"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#普通方法实现\n",
    "num = [1,2,3,4,5]\n",
    "new = []\n",
    "for i in num:\n",
    "    new.append(i**2)\n",
    "new    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 4, 9, 16, 25]"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#列表生成式\n",
    "num = [1,2,3,4,5]\n",
    "[i**2 for i in num]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "现有两列表，需要列表的值两两组合"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Aa', 'Ab', 'Ac', 'Ba', 'Bb', 'Bc', 'Ca', 'Cb', 'Cc']"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#普通方法实现\n",
    "list1 = [\"A\",\"B\",\"C\"]\n",
    "list2 = [\"a\",\"b\",\"c\"]\n",
    "new =[]\n",
    "for m in list1:\n",
    "    for n in list2:\n",
    "        new.append(m+n)\n",
    "new        \n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Aa', 'Ab', 'Ac', 'Ba', 'Bb', 'Bc', 'Ca', 'Cb', 'Cc']"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#列表生成式\n",
    "list1 = [\"A\",\"B\",\"C\"]\n",
    "list2 = [\"a\",\"b\",\"c\"]\n",
    "[m + n for m in list1 for n in list2]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### map函数  \n",
    "- map函数的表现形式是map(function,agrs), 表示对序列 args 中的每个值进行function操作，最终得到一个结果序列\n",
    "- map函数生成的结果序列不会直接显示出来，想要获得结果需要for循环遍历取出，或者用list方法生成一个列表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<map at 0x23e0d3b75c0>"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = map(lambda x,y:x+y,[1,2,3],[3,2,1])\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "4\n",
      "4\n",
      "4\n"
     ]
    }
   ],
   "source": [
    "for i in a:\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[4, 4, 4]"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b = list(map(lambda x,y:x+y,[1,2,3],[3,2,1]))\n",
    "b"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 模块  \n",
    "模块是升级版的函数，数据分析用的比较多的三个模块分别是NumPy 、Pandas、matplotlib"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "",
   "title_sidebar": "第二章 Python基础知识",
   "toc_cell": false,
   "toc_position": {
    "height": "calc(100% - 180px)",
    "left": "10px",
    "top": "150px",
    "width": "320px"
   },
   "toc_section_display": true,
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
